using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace IGlobalist.Cms.Common 
{
    public class SiteConfig
    {
        private static object lockObj = new object();
        private static XmlDocument xDoc = null;
        private static string path = null;
        static SiteConfig()
        {
        }
        public static long SiteAccessNumber
        {
            get
            {
                return Convert.ToInt64(getSettingNode("SiteAccessNumber").Attributes["value"].Value);
            }
            set
            {
                lock (lockObj = new object())
                {
                    getSettingNode("SiteAccessNumber").Attributes["value"].Value = value.ToString();
                    
                }
            }
        }

        public static string SiteFooter
        {
            get
            {
                return getSettingNode("SiteFooter").Attributes["value"].Value;
            }
            set
            {
                getSettingNode("SiteFooter").Attributes["value"].Value = value;
            }
        }

        public static string SiteTitle
        {
            get
            {
                return getSettingNode("SiteTitle").Attributes["value"].Value;
            }
            set
            {
                getSettingNode("SiteTitle").Attributes["value"].Value = value;
            }
        }

        public static string MetaKeyWords
        {
            get
            {
                return getSettingNode("MetaKeyWords").Attributes["value"].Value;
            }
            set
            {
                getSettingNode("MetaKeyWords").Attributes["value"].Value = value;
            }
        }

        public static string MetaDescription
        {
            get
            {
                return getSettingNode("MetaDescription").Attributes["value"].Value;
            }
            set
            {
                getSettingNode("MetaDescription").Attributes["value"].Value = value;
            }
        }

        public static string Payment
        {
            get
            {
                return getSettingNode("Payment").Attributes["value"].Value;
            }
            set
            {
                getSettingNode("Payment").Attributes["value"].Value = value;
            }
        }

        private static XmlNode getSettingNode(string key)
        {
            return getRootConfig().SelectSingleNode(string.Format("Setting[@key='{0}']", key));
        }

        private static XmlElement getRootConfig()
        {
            if (xDoc == null)
            {
                path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/SiteConfig.config");
                xDoc = new XmlDocument();
                xDoc.Load(path);
            }
            return xDoc.DocumentElement;
        }

        public static void SaveConfig()
        {
            lock (lockObj = new object())
            {                
                xDoc.Save(path);
            }
        }
    }
}
